package com.hopu.v1.controller.admin.subject;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.v1.pojo.common.PageData;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.common.Update;
import com.hopu.v1.pojo.subject.Subject;
import com.hopu.v1.service.admin.subject.SubjectService;
import com.hopu.v1.utils.JwtUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 学科表(Subject)表控制层
 *
 * @author makejava
 * @since 2024-09-25 15:03:58
 */
@RestController
@RequestMapping("/api/v1/admin/subject")
@CrossOrigin
public class SubjectController{
    /**
     * 服务对象
     */
    @Resource
    private SubjectService subjectService;



    /**
     * 根据条件查询所有数据
     *
     * @param subject 实例对象
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Subject subject) {
        return R.ok(subjectService.list(new QueryWrapper<>(subject)));
    }

    /**
     * 分页所有数据
     *
     * @param currentPage 当前页
     * @param pageSize 每页大小
     * @return 所有数据
     */
    @GetMapping("/list")
    public R selectAll(Integer currentPage, Integer pageSize) {
        if (pageSize == -1){
            return R.ok(subjectService.page(new Page<>(currentPage,pageSize)));
        }
        PageData<Subject> subjectPageData = subjectService.selectAll(currentPage, pageSize);
        return R.ok("数据获取成功",subjectPageData);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.subjectService.getById(id));
    }

    @GetMapping("getSubjectById")
    public R getRoleById(@RequestParam("subjectId") Integer subjectId){
        return R.ok(this.subjectService.query().select("id","subject_name").list());
    }

    /**
     * 根据token获取员工id
     * @param request 请求
     * @return 员工id
     */
    Integer getStaffIdByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        String staffId = JwtUtils.getUserId(token);
        return Integer.parseInt(staffId);
    }

    /**
     * 新增数据
     * @param request 请求
     * @param subject 实体对象
     * @return 新增消息
     */
    @PostMapping
    public R insert(HttpServletRequest request, Subject subject) {
        subject.setCreationTime(LocalDateTime.now());
        //根据token解析出staffId
        Integer staffId = getStaffIdByToken(request);
        subject.setCreatorId(staffId);

        //添加
        subjectService.add(subject);

        return R.ok("添加成功");
    }

    /**
     * 修改数据
     * @param subject 实体对象
     * @param request 请求
     * @return 修改消息
     */
    @PutMapping
    public R update(@Validated(Update.class) Subject subject,HttpServletRequest request) {
        subject.setUpdateTime(LocalDateTime.now());
        Integer staffId = getStaffIdByToken(request);
        subject.setUpdateId(staffId);

        this.subjectService.update(subject);
        return R.ok("修改成功");
    }

    @PutMapping("/updateStatus")
    public R updateStatus(@RequestBody Subject subject) {
        subjectService.updateStatus(subject);
        return R.ok("修改成功");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.subjectService.removeByIds(idList));
    }
    
    /**
     * 模糊查询学科
     * @param subjectName
     * @return
     */
    @GetMapping("/name")
    public R getSubjectByName(String subjectName){
        return R.ok(subjectService.getSubjectByName(subjectName));
    }

    @GetMapping("/getSubjectByStaffId")
    public R getSubjectByStaffId(@RequestParam ("empId") Integer empId){
        return R.ok(this.subjectService.getSubjectByStaffId(empId));
    }
}

